﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;


namespace VFTray
{
    class ThreadSafeDisposable
    {
        private const int DisposalNotStarted = 0;
        private const int DisposalComplete = 1;
        private int _disposeStage = 0;

        public bool IsDisposed => Interlocked.CompareExchange(ref _disposeStage, DisposalComplete, DisposalComplete) == DisposalComplete;

        internal bool SafeDispose()
        {
            if (Interlocked.Exchange(ref _disposeStage, DisposalComplete) != DisposalNotStarted)
            {
                return false;
            }
            return true;
        }
    }
}
